package com.kly.sc.api.application.controller.config;

import com.fqgj.common.api.Response;
import com.fqgj.common.utils.CollectionUtils;
import com.kly.sc.api.application.annos.SignIgnore;
import com.kly.sc.api.application.annos.VisitorAccessible;
import com.kly.sc.api.application.controller.config.request.RegisterConfigRequest;
import com.kly.sc.api.application.controller.config.response.RegisterConfigResponse;
import com.kly.sc.api.application.controller.config.response.RegisterConfigVo;
import com.kly.sc.api.application.helper.RequestLocalInfo;
import com.kly.sc.api.application.utils.Result;
import com.kly.sc.api.application.utils.RpcUtils;
import com.kly.user.dto.RegisterConfigDto;
import com.kly.user.service.OtherService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author linqu
 */
@RestController
@RequestMapping("/api/app/")
@Slf4j
@Api(value = "登陆、注册页面初始化加载选项配置", tags = {"登陆、注册页面初始化加载选项配置"})
public class RegisterSetupController {

    private final Logger logger = LoggerFactory.getLogger(getClass());
    @Resource
    OtherService otherService;

    /**
     * 登陆&注册页面加载相关配置
     */
    @SignIgnore
    @VisitorAccessible
    @ResponseBody
    @PostMapping("/register/config")
    @ApiOperation(value = "登陆、注册页面初始化加载选项配置", notes = "登陆、注册页面初始化加载选项配置")
    public Result<RegisterConfigResponse> config(@RequestBody RegisterConfigRequest configRequest) {

        final String appCode = RequestLocalInfo.getRequestBasicInfo().getAppCode();
        final List<RegisterConfigDto> registerConfigDtos = RpcUtils.getResult(otherService.getRegisterConfigs(appCode));
        if (CollectionUtils.isEmpty(registerConfigDtos)) {
            return Result.ok();
        }
        final List<RegisterConfigVo> registerConfigVoList = registerConfigDtos.stream().map(o -> {
            RegisterConfigVo vo = new RegisterConfigVo();
            BeanUtils.copyProperties(o, vo);
            return vo;
        }).collect(Collectors.toList());

        return Result.ok(RegisterConfigResponse.builder().configs(registerConfigVoList).build());
    }



}
